{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import hashlib\n",
    "import rabbitpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Open the connection and the channel\n",
    "connection = rabbitpy.Connection()\n",
    "channel = connection.channel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the worker queue\n",
    "queue_name = 'hashing-worker-%s' % os.getpid()\n",
    "queue = rabbitpy.Queue(channel, queue_name,\n",
    "                       auto_delete=True,\n",
    "                       durable=False,\n",
    "                       exclusive=True)\n",
    "\n",
    "# Declare the worker queue\n",
    "if queue.declare():\n",
    "    print('Worker queue declared')\n",
    "\n",
    "# Bind the worker queue\n",
    "if queue.bind('fanout-rpc-requests'):\n",
    "    print('Worker queue bound')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Consume messages from RabbitMQ\n",
    "for message in queue.consume_messages():\n",
    "    \n",
    "    # Create the hashing object\n",
    "    hash_obj = hashlib.md5(message.body)\n",
    "    \n",
    "    # Print out the info, this might go into a database or log file\n",
    "    print('Image with correlation-id of %s has a hash of %s' % \n",
    "          (message.properties['correlation_id'], \n",
    "           hash_obj.hexdigest()))\n",
    "    \n",
    "    # Acknowledge the delivery of the RPC request message\n",
    "    message.ack()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
